package com.jzoffer;

public class ReverseList {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(9);
        ListNode l2 = new ListNode(5);
        ListNode l3 = new ListNode(49);
        ListNode l4 = new ListNode(79);
        ListNode l5 = new ListNode(3249);

        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        l4.next = l5;

        ListNode node = ReverseList(l1);
        while (node != null){
            System.out.println(node.val);
            node = node.next;
        }
    }

    public static ListNode ReverseList(ListNode head){
        if(head == null || head.next == null){
            return head;
        }
        ListNode dummy = new ListNode(-1039);

        while (head != null){
            ListNode nextNode = head.next;
            head.next = dummy.next;
            dummy.next = head;
            head = nextNode;
        }
        return dummy.next;
    }
}
